package com.hu.gulimallproduct.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.to.SkuProductTo;
import com.hu.gulimallcommon.to.SpuBoundsTo;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallcommon.utils.R;
import com.hu.gulimallproduct.project.dao.PmsSpuInfoDao;
import com.hu.gulimallproduct.project.entity.*;
import com.hu.gulimallproduct.project.feign.CouponFeginService;
import com.hu.gulimallproduct.project.service.*;
import com.hu.gulimallproduct.project.service.PmsSpuInfoService;
import com.hu.gulimallproduct.project.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("pmsSpuInfoService")
public class PmsSpuInfoServiceImpl extends ServiceImpl<PmsSpuInfoDao, PmsSpuInfoEntity> implements PmsSpuInfoService {
    @Autowired
    PmsSpuInfoDescService spuInfoDescService;
    @Autowired
    PmsSpuImagesService spuImagesService;
    @Autowired
    PmsProductAttrValueService productAttrValueService;
    @Autowired
    PmsAttrService attrService;
    @Autowired
    PmsSkuInfoService skuInfoService;
    @Autowired
    PmsSkuImagesService skuImagesService;
    @Autowired
    PmsSkuSaleAttrValueService saleAttrValueService;
    @Autowired
    CouponFeginService couponFeginService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PmsSpuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String)params.get("key");
        if(StringUtils.isNotEmpty( key)){
            queryWrapper.and((item)->{
                item.eq("id",key)
                        .or()
                        .like("spu_name",key);
            });
        }
        String catelogId = (String)params.get("catelogId");
        if(StringUtils.isNotEmpty( catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String)params.get("brandId");
        if(StringUtils.isNotEmpty( brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        String status = (String)params.get("status");
        if(StringUtils.isNotEmpty( status)){
            queryWrapper.eq("publish_status",status);
        }
        IPage<PmsSpuInfoEntity> page = this.page(
                new Query<PmsSpuInfoEntity>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuSaveVo vo) {

        //1.保存spu基本信息
        PmsSpuInfoEntity spuInfo = new PmsSpuInfoEntity();
        BeanUtils.copyProperties(vo,spuInfo);
        this.save(spuInfo);
        //2.保存spu的描述图片
        List<String> decript = vo.getDecript();
        PmsSpuInfoDescEntity infoDescEntity = new PmsSpuInfoDescEntity();
        infoDescEntity.setSpuId(spuInfo.getId());
        infoDescEntity.setDecript(String.join(",",decript));
        spuInfoDescService.save(infoDescEntity);
        //3.保存spu的图片集
        List<String> images = vo.getImages();
        if(images!=null && images.size()>0){
            List<PmsSpuImagesEntity> collect = images.stream().map(item -> {
                PmsSpuImagesEntity imagesEntity = new PmsSpuImagesEntity();
                imagesEntity.setSpuId(spuInfo.getId());
                imagesEntity.setImgUrl(item);
                return imagesEntity;
            }).collect(Collectors.toList());
            spuImagesService.saveBatch(collect);
        }
        //4.保存spu的规格参数
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<PmsProductAttrValueEntity> collect = baseAttrs.stream().map(item -> {
            PmsProductAttrValueEntity attrValueEntity = new PmsProductAttrValueEntity();
            attrValueEntity.setSpuId(spuInfo.getId());
            attrValueEntity.setAttrId(item.getAttrId());
            attrValueEntity.setAttrName(attrService.getById(item.getAttrId()).getAttrName());
            attrValueEntity.setAttrValue(item.getAttrValues());
            attrValueEntity.setQuickShow(item.getShowDesc());
            return attrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveBatch(collect);
        //5.保存spu的所有sku信息
        //5.6.保存sku的积分信息 gulimall sms
        Bounds bounds = vo.getBounds();
        SpuBoundsTo spuBoundsTo = new SpuBoundsTo();
        BeanUtils.copyProperties(bounds,spuBoundsTo);
        spuBoundsTo.setSpuId(spuInfo.getId());
        couponFeginService.saveSpuBounds(spuBoundsTo);
        //5.1.保存sku基本信息
        List<Skus> skus = vo.getSkus();
        if(skus!= null && skus.size()>0){
            skus.forEach(item -> {
                PmsSkuInfoEntity skuInfoEntity = new PmsSkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfo.getBrandId());
                skuInfoEntity.setCatalogId(spuInfo.getCatalogId());
                skuInfoEntity.setSpuId(spuInfo.getId());
                for (Images image : item.getImages()) {
                    if(image.getDefaultImg()==1){
                        skuInfoEntity.setSkuDefaultImg(image.getImgUrl());
                    }
                }
                skuInfoEntity.setSkuDefaultImg("");
                skuInfoService.save(skuInfoEntity);
                Long skuId = skuInfoEntity.getSkuId();
                //5.3.保存sku的图片信息
                List<PmsSkuImagesEntity> skuImagesEntities = item.getImages().stream().map(item1 -> {
                    PmsSkuImagesEntity skuImagesEntity = new PmsSkuImagesEntity();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgUrl(item1.getImgUrl());
                    skuImagesEntity.setDefaultImg(item1.getDefaultImg());
                    return skuImagesEntity;
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(skuImagesEntities);
                //5.2.保存sku的销售属性信息
                List<PmsSkuSaleAttrValueEntity> attrEntities = item.getAttr().stream().map(item1 -> {
                    PmsSkuSaleAttrValueEntity entity = new PmsSkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(item1,entity);
                    entity.setSkuId(skuId);
                    return entity;
                }).collect(Collectors.toList());
                saleAttrValueService.saveBatch(attrEntities);
                //5.4.保存sku的优惠信息满减信息
                SkuProductTo skuProductTo = new SkuProductTo();
                BeanUtils.copyProperties(item,skuProductTo);
                skuProductTo.setSkuId(skuId);
                R r = couponFeginService.saveSkuReduction(skuProductTo);
                if(r.getCode()!=0){
                    log.error("远程保存sku优惠信息失败");
                }

            });
        }





    }
}